package sz.szsmk.citizencard.mvp.dzsmkpresenter;


import com.google.gson.Gson;

import javax.inject.Inject;

import rx.functions.Action1;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.dzbean.req.CheckPwdRequest;
import sz.szsmk.citizencard.model.network.dzbean.req.MoneyRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.GetMyBankResponse;
import sz.szsmk.citizencard.mvp.RechargeMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

/**
 * Created by yangzhengwen on 2017/10/4.
 */

public class RechargePresenter extends AppBasePresenter<RechargeMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;

    @Inject
    public RechargePresenter() {
        getComponent().inject(this);
    }

    public void checkPayPassword(String password, final String type) {
        checkViewAttached();
        CheckPwdRequest checkPwdRequest = new CheckPwdRequest();
        checkPwdRequest.setPassword(password);
        ObjectSignatureUtil.signatureRequest(checkPwdRequest);
        mNetworkDateSource.checkPayPassword(checkPwdRequest, new Action1<GetMyBankResponse>() {
            @Override
            public void call(GetMyBankResponse getMyBankResponse) {
                String code = getMyBankResponse.getCode();
                if (code.equals("0000")) {
                    getMvpView().onCheckSuccess(type);
                } else {
                    getMvpView().onCheckFailed(getMyBankResponse);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    public void recharge(int money, String bankCardId) {
        checkViewAttached();
        HttpApi httpApi = mNetworkDateSource.getHttpApi();
        MoneyRequest moneyRequest = new MoneyRequest();
        moneyRequest.setMoney(money);
        moneyRequest.setBankCardId(bankCardId);
        ObjectSignatureUtil.signatureRequest(moneyRequest);
        String json = new Gson().toJson(moneyRequest);
        L.e(Constant.MTAG,json);
        mNetworkDateSource.recharge(moneyRequest, new Action1<GetCodeResponse>() {
            @Override
            public void call(GetCodeResponse getCodeResponse) {
                String code = getCodeResponse.getCode();
                if (code.equals("0000")) {
                    getMvpView().onRechargeSuccess();
                } else {
                    getMvpView().onRechargeFailed(getCodeResponse);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });


    }
}
